//
//  HNTeacherBusinessMoneyHeaderView.m
//  优学荟管理系统
//
//  Created by edz on 2017/11/18.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "HNTeacherBusinessMoneyHeaderView.h"

@implementation HNTeacherBusinessMoneyHeaderView
{
    __weak IBOutlet UIButton *_BeginTimeBtn;
    __weak IBOutlet UIButton *_EndTimeBtn;
    __weak IBOutlet UIButton *_SureBtn;
    /** 以下为时间控件所需参数、属性 */
    NSInteger _mark;//标记类型
    NSString *_selectDate;//标记选中的日期
    CustomDayDatePicker *_DatePicker;
    void (^_CallBack)(NSString *, NSString *, UIButton *);
}

/** 实例化，Block传出开始时间、结束时间以及确定按钮*/
+ (instancetype)ConfigHeaderViewOfSureAction:(void (^)(NSString *, NSString *, UIButton *))CallBack {
    
    return [[self alloc] initWithSureAction:CallBack];
}
- (instancetype)initWithSureAction:(void (^)(NSString *BeginTimeStr, NSString *EndTimeStr, UIButton *SureBtn))CallBack {
    
    self = [[NSBundle mainBundle] loadNibNamed:[HNTeacherBusinessMoneyHeaderView description] owner:nil options:nil][0];
    if (self) {
     
        _CallBack = CallBack;
        [self datePicker];
        [_BeginTimeBtn setTitle:[NSString yearMonthDay] forState:(UIControlStateNormal)];
        [_EndTimeBtn setTitle:[NSString yearMonthDay] forState:(UIControlStateNormal)];
        self.frame = CGRectMake(0, 0, kScreenWidth, 211);
    }
    return self;
}


#pragma mark - Action
/** 开始时间事件 */
- (IBAction)Action_BeginTime:(UIButton *)sender {
 
    [self ClickTimeOfMark:1 Title:@"开始时间"];
}

/** 结束时间事件 */                              
- (IBAction)Action_EndTime:(UIButton *)sender {
    
    [self ClickTimeOfMark:2 Title:@"结束时间"];
}

/** 确定搜索 */
- (IBAction)Action_Sure:(UIButton *)sender {
    
    
    NSComparisonResult result = [_BeginTimeBtn.currentTitle compare:_EndTimeBtn.currentTitle];
    
    if (result != NSOrderedAscending) {
        [SVProgressHUD showErrorWithStatus:@"结束时间必须大于开始时间!"];
    }else{
       _CallBack ? _CallBack(_BeginTimeBtn.currentTitle, _EndTimeBtn.currentTitle, sender) : nil;
    }

    
    
    
    
}

#pragma mark - Func
- (void)ClickTimeOfMark:(NSInteger)Mark Title:(NSString *)Title {
    JXAlertview *alert = [[JXAlertview alloc]initWithFrame:CGRectMake(30, kScreenHeight/2-100, kScreenWidth - 60, 200)];
    alert.delegate = self;
    [alert initwithtitle:Title andcommitbtn:@"确定" andStr:@"0"];
    [alert addSubview:self.datePicker];
    [alert show];
    
    
    _mark = Mark;
}

#pragma mark - 时间控件的代理方法
- (void)btnindex:(int)index :(int)tag{
    
    if (index == 2) {
        if (_mark == 1) {
            _selectDate = [NSString stringWithFormat:@"%d-%02d-%02d",_DatePicker.year,_DatePicker.month,_DatePicker.day];
            [_BeginTimeBtn setTitle:_selectDate forState:(UIControlStateNormal)];
        }else if (_mark == 2){
            
            _selectDate = [NSString stringWithFormat:@"%d-%02d-%02d",_DatePicker.year,_DatePicker.month,_DatePicker.day];
            [_EndTimeBtn setTitle:_selectDate forState:(UIControlStateNormal)];
        }
    }
    
    
    
    
}

#pragma mark - Get
- (CustomDayDatePicker *)datePicker {
    
    if (_DatePicker == nil) {
        _DatePicker = [[CustomDayDatePicker alloc]initWithFrame:CGRectMake(0, 20, kScreenWidth - 60, 140)];
    }
    return _DatePicker;
}
@end
